<?
$base_db="bakapix";
$host_db="sql12";
$user_db="bakapix";
$pass_db="v24zx46m";

mysql_connect($host_db,$user_db,$pass_db);
mysql_select_db($base_db);

$prefix="WGM_";
$table=$_REQUEST["t"];

$res=mysql_query("select * from ".$prefix.$table);

$n=mysql_num_fields($res);

echo "class ".$table."{\n\n";
for ($i=0;$i<$n;$i++){
	echo "\tvar $".mysql_field_name($res,$i).";\n";
}
echo "\n";

// Constructeur
echo "\tfunction ".$table."(\$var){\n";
echo "\t\tif(is_object(\$var)){\n";
for ($i=0;$i<$n;$i++){
	$field=mysql_field_name($res,$i);
	echo "\t\t\t\$this->".$field."=\$var->".$field.";\n";
}
echo "\t\t}\n";
echo "\t\tif(is_array(\$var)){\n";
for ($i=0;$i<$n;$i++){
	$field=mysql_field_name($res,$i);
	echo "\t\t\t\$this->".$field."=\$var[\"".$field."\"];\n";
}
echo "\t\t}\n";
echo "\t}\n\n";

// Accesseurs
for ($i=0;$i<$n;$i++){
	$field=mysql_field_name($res,$i);
	echo "\tfunction get_".$field."(){ return stripslashes(\$this->".$field."); }\n";
	echo "\tfunction set_".$field."(\$value){ \$this->".$field."=\$value; }\n";
}

echo "\n\n";

// Fonction d'ajout
echo "\tfunction ajouter(){\n";
echo "\t\tglobal \$sql;\n";
echo "\t\t\$sqlr=\"insert into \".CFG_MYSQL_PREFIX.\"".$table."(";
$tmp="";
for ($i=1;$i<$n;$i++) $tmp.=mysql_field_name($res,$i).",";
echo substr($tmp,0,(strlen($tmp)-1));
echo ") values(";
$tmp="";
for ($i=1;$i<$n;$i++) $tmp.="'\".addslashes(\$this->".(mysql_field_name($res,$i)).").\"'".",";
echo substr($tmp,0,(strlen($tmp)-1));
echo")\";\n";
echo "\t\t\$sql->query(\$sqlr,0);\n";
echo "\t}\n\n";

// Fonction de mise a jour
echo "\tfunction maj(){\n";
echo "\t\tglobal \$sql;\n";
echo "\t\t\$sql->query(\"update \".CFG_MYSQL_PREFIX.\"".$table." set ";
$tmp="";
for ($i=1;$i<$n;$i++){
	$tmp.=mysql_field_name($res,$i)."='\".addslashes(\$this->".(mysql_field_name($res,$i)).").\"',";
}
echo substr($tmp,0,(strlen($tmp)-1));
echo " where ".mysql_field_name($res,0)."=\".\$this->".mysql_field_name($res,0).",0);\n";
echo "\t}\n\n";

echo "}";
?>